package cn.zhentao.controller;

import cn.zhentao.mapper.RoleMapper;
import cn.zhentao.mapper.UsersMapper;
import cn.zhentao.pojo.Role;
import cn.zhentao.pojo.Userrole;
import cn.zhentao.pojo.Users;
import cn.zhentao.service.IRoleService;
import cn.zhentao.service.IUserroleService;
import cn.zhentao.util.JwtService;
import cn.zhentao.util.Result;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private IUserroleService iUserroleService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private RoleMapper roleMapper;

    public UserController() {
    }

    @RequestMapping({"login"})
    public Result login(@RequestBody Users users) {
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper();
        usersQueryWrapper.eq("username", users.getUsername());
        System.err.println("users:++++++++++++++++" + users);
        Users user = (Users)this.usersMapper.selectOne(usersQueryWrapper);
        System.err.println("user:++++++++++++++++++++" + user);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(users.getPassword(), user.getPassword());
        if (user != null && matches) {
            List<Userrole> id = this.iUserroleService.list((Wrapper)(new QueryWrapper()).eq("uid", user.getId()));
            if (id != null && id.size() > 0) {
                Iterator var7 = id.iterator();

                while(var7.hasNext()) {
                    Userrole userrole = (Userrole)var7.next();
                    Role rid = (Role)this.roleMapper.selectOne((Wrapper)(new QueryWrapper()).eq("rid", userrole.getRid()));
                    user.setRname(rid.getRname());
                }
            }

            HashMap<String, Object> map = new HashMap();
            map.put("username", user.getUsername());
            map.put("nikename", user.getNickname());
            map.put("id", user.getId());
            map.put("rname", user.getRname());
            String token = JwtService.createToken(map);
            System.err.println("rname:-------------" + user.getRname());
            return Result.OK(token);
        } else {
            return Result.ERROR("登录方法错误");
        }
    }
    //头像上传
    @RequestMapping("upload")
    public Result upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//上传的文件的名字
        String path = "C:/Users/86156/Desktop/MyProect/myProjectImages/images/pic/";
        //System.out.println(originalFilename);
        //文件重命名
        int i = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(i); // .jpg
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        String newname = name+extname;
        File f = new File(path,newname);
        file.transferTo(f);//复制
        String url = "http://localhost:8080/images/pic/"+newname;
        return Result.OK(url);
    }
}
